/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.jfonia.view.main.header;

import javax.swing.JLabel;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfonia.connect5.basics.Observer;
import org.jfonia.constants.SymbolConstants;
import org.jfonia.constants.ViewConstants;
import org.jfonia.language.LabelConstants;
import org.jfonia.language.LanguageResource;
import org.jfonia.notation.Notation;

/**
 *
 * @author Rik Bauwens
 */
public class SymbolPanel extends NotationPanel
{
        public SymbolPanel()
        {
                super(LanguageResource.getInstance().getLabel(LabelConstants.SYMBOLS));
        }

        @Override
        protected void init()
        {
                JLabel clefLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.CLEF));
                addLabel(clefLabel);
                GlyphLabel gClef = new GlyphLabel(this, SymbolConstants.G_CLEF,
                                                      NotationType.CLEF, ViewConstants.G_CLEF);
                addOption(gClef);
                GlyphLabel fClef = new GlyphLabel(this, SymbolConstants.F_CLEF,
                                                      NotationType.CLEF, ViewConstants.F_CLEF);
                addOption(fClef);
                GlyphLabel cClef = new GlyphLabel(this, SymbolConstants.C_CLEF,
                                                      NotationType.CLEF, ViewConstants.C_CLEF);
                addOption(cClef);
                GlyphLabel gM8Clef = new GlyphLabel(this, SymbolConstants.G_MINUS8_CLEF,
                                                      NotationType.CLEF, ViewConstants.G_MINUS8_CLEF);
                addOption(gM8Clef);
                GlyphLabel gP8Clef = new GlyphLabel(this, SymbolConstants.G_PLUS8_CLEF,
                                                      NotationType.CLEF, ViewConstants.G_PLUS8_CLEF);
                addOption(gP8Clef);
                GlyphLabel fM8Clef = new GlyphLabel(this, SymbolConstants.F_MINUS8_CLEF,
                                                      NotationType.CLEF, ViewConstants.F_MINUS8_CLEF);
                addOption(fM8Clef);
                GlyphLabel fP8Clef = new GlyphLabel(this, SymbolConstants.F_PLUS8_CLEF,
                                                      NotationType.CLEF, ViewConstants.F_PLUS8_CLEF);
                addOption(fP8Clef);
                addSpacing();

                JLabel keySignatureLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.KEY_SIGNATURE));
                addLabel(keySignatureLabel);
                GlyphLabel accNatural = new GlyphLabel(this, SymbolConstants.ACC_NATURAL,
                                                           NotationType.KEY_SIGNATURE,
                                                           ViewConstants.ACC_NATURAL);
                addOption(accNatural);
                GlyphLabel accFlat = new GlyphLabel(this, SymbolConstants.ACC_FLAT,
                                                       NotationType.KEY_SIGNATURE,
                                                       ViewConstants.ACC_FLAT);
                addOption(accFlat);
                GlyphLabel accSharp = new GlyphLabel(this, SymbolConstants.ACC_SHARP,
                                                       NotationType.KEY_SIGNATURE,
                                                       ViewConstants.ACC_SHARP);
                addOption(accSharp);
                final SpinnerModel keySignatureModel =
                        new SpinnerNumberModel(Notation.getInstance().getAbsoluteKeySignature(),
                                               ViewConstants.MIN_KEY_SIGNATURE,
                                               ViewConstants.MAX_KEY_SIGNATURE,
                                               ViewConstants.STEP_KEY_SIGNATURE);
                final CustomSpinner keySignature = new CustomSpinner(keySignatureModel);
                Notation.getInstance().getKeySignatureNode().addObserver(new Observer(){
                        public void onNotify(Object source)
                        {
                                if(((Integer) keySignatureModel.getValue()).intValue() != Notation.getInstance().getAbsoluteKeySignature())
                                        keySignatureModel.setValue(Notation.getInstance().getAbsoluteKeySignature());
                                keySignature.setEnabled(Notation.getInstance().getKeySignatureDirection() != 0);
                        }
                });
                keySignatureModel.addChangeListener(new ChangeListener(){
                        public void stateChanged(ChangeEvent e)
                        {
                                if(Notation.getInstance().getAbsoluteKeySignature() != ((Integer) keySignatureModel.getValue()).intValue())
                                        Notation.getInstance().setAbsoluteKeySignature((Integer) keySignatureModel.getValue());
                        }
                });
                keySignature.setEnabled(Notation.getInstance().getKeySignatureDirection() != 0);
                keySignature.setMaximumSize(keySignature.getPreferredSize());
                addOption(keySignature);
                addSpacing();

                JLabel timeSignatureLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.TIME_SIGNATURE));
                addLabel(timeSignatureLabel);
                final SpinnerModel nominatorModel =
                        new SpinnerNumberModel(Notation.getInstance().getNominator(),
                                               ViewConstants.MIN_DENOMINATOR,
                                               ViewConstants.MAX_NOMINATOR,
                                               ViewConstants.STEP_NOMINATOR);
                final CustomSpinner nominator = new CustomSpinner(nominatorModel);
                Notation.getInstance().getNominatorNode().addObserver(new Observer(){
                        public void onNotify(Object source)
                        {
                                if(((Integer) nominatorModel.getValue()).intValue() != Notation.getInstance().getNominator())
                                        nominatorModel.setValue(Notation.getInstance().getNominator());
                        }
                });
                nominatorModel.addChangeListener(new ChangeListener(){
                        public void stateChanged(ChangeEvent e)
                        {
                                if(Notation.getInstance().getNominator() != ((Integer) nominatorModel.getValue()).intValue())
                                        Notation.getInstance().setNominator((Integer) nominatorModel.getValue());
                        }
                });
                nominator.setMaximumSize(nominator.getPreferredSize());
                addOption(nominator);
                final SpinnerModel denominatorModel =
                        new SpinnerNumberModel(Notation.getInstance().getDenominator(),
                                               ViewConstants.MIN_DENOMINATOR,
                                               ViewConstants.MAX_DENOMINATOR,
                                               ViewConstants.STEP_DENOMINATOR);
                final CustomSpinner denomintor = new CustomSpinner(denominatorModel);
                Notation.getInstance().getDenominatorNode().addObserver(new Observer(){
                        public void onNotify(Object source)
                        {
                                if(((Integer) denominatorModel.getValue()).intValue() != Notation.getInstance().getDenominator())
                                        denominatorModel.setValue(Notation.getInstance().getDenominator());
                        }
                });
                denominatorModel.addChangeListener(new ChangeListener(){
                        public void stateChanged(ChangeEvent e)
                        {
                                if(Notation.getInstance().getDenominator() != ((Integer) denominatorModel.getValue()).intValue())
                                        Notation.getInstance().setDenominator((Integer) denominatorModel.getValue());
                        }
                });
                denomintor.setMaximumSize(denomintor.getPreferredSize());
                addOption(denomintor);
                addSpacing();

                JLabel barLabel = new JLabel(LanguageResource.getInstance().
                        getLabel(LabelConstants.BAR));
                addLabel(barLabel);
                final SpinnerModel barModel =
                    new SpinnerNumberModel(Notation.getInstance().getBar(),
                                           ViewConstants.MIN_BAR,
                                           ViewConstants.MAX_BAR,
                                           ViewConstants.STEP_BAR);
                final CustomSpinner bar = new CustomSpinner(barModel);
                Notation.getInstance().getBarNode().addObserver(new Observer(){
                        public void onNotify(Object source)
                        {
                                if(((Integer) barModel.getValue()).intValue() != Notation.getInstance().getBar())
                                        barModel.setValue(Notation.getInstance().getBar());
                        }
                });
                barModel.addChangeListener(new ChangeListener(){
                        public void stateChanged(ChangeEvent e)
                        {
                                if(Notation.getInstance().getBar() != ((Integer) barModel.getValue()).intValue())
                                        Notation.getInstance().setBar((Integer) barModel.getValue());
                        }
                });
                bar.setMaximumSize(bar.getPreferredSize());
                addOption(bar);
        }
}
